package org.zcl.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.related.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zcl.entity.po.TbUserInfo;
import org.zcl.entity.request.TbUserRequest;
import org.zcl.entity.response.UserVO;
import org.zcl.entity.model.ApiResponse;
import org.zcl.entity.model.PageResponse;
import org.zcl.service.TbUserInfoService;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户控制器
 *
 * @author zhuchenglin
 * @date 2022/09/01 17:07
 */
@Api(value = "/user/", tags = {"用户控制器"})
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController extends BaseAction {

    /**
     * 演示服务
     */
    @Resource(name = "demo")
    private DemoService demoService;

    /**
     * 结核病用户信息服务
     */
    @Autowired
    private TbUserInfoService tbUserInfoService;

    /**
     * 说什么
     *
     * @return {@link String}
     */
    @ApiOperation(value = "说什么", notes = "说什么", httpMethod = "GET")
    @GetMapping("say")
    public String sayWhat() {
        return demoService.say();
    }

    /**
     * 得到
     * 获取
     *
     * @param id id
     * @return {@link String}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "id", value = "id")
    })
    @ApiOperation(value = "获取", notes = "获取", httpMethod = "GET")
    @GetMapping("get")
    public String get(@RequestParam("id") Integer id) {
        return "User:" + id;
    }

    /**
     * 添加
     *
     * @param userVO 用户签证官
     * @return {@link ApiResponse}<{@link ?}>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserVO", name = "userVO", value = "用户签证官")
    })
    @ApiOperation(value = "添加", notes = "添加", httpMethod = "POST")
    @PostMapping("add")
    public ApiResponse<?> add(@RequestBody @Valid UserVO userVO) {
        ApiResponse.builder().build();
        return ApiResponse.builder().data(JSONObject.toJSONString(userVO)).code("1").msg("success").build();
    }

    /**
     * 更新
     *
     * @param userVO 用户签证官
     * @return {@link ApiResponse}<{@link UserVO}>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserVO", name = "userVO", value = "用户签证官")
    })
    @ApiOperation(value = "更新", notes = "更新", httpMethod = "POST")
    @PostMapping("update")
    public ApiResponse<UserVO> update(@RequestBody @Valid UserVO userVO) {
        log.info("请求入参:{}", userVO);
        ApiResponse.builder().build();
        for (int i = 0; i < 100; i++) {
            userVO.setAge(userVO.getAge() + i);
            userVO.setName(String.valueOf(i));
            tbUserInfoService.update(userVO);
        }
        return ApiResponse.<UserVO>builder().data(userVO).code("1").msg("success").build();
    }


    /**
     * 页面
     *
     * @param request 请求
     * @return {@link ApiResponse}<{@link PageResponse}<{@link TbUserInfo}>>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "TbUserRequest", name = "request", value = "请求")
    })
    @ApiOperation(value = "页面", notes = "页面", httpMethod = "POST")
    @PostMapping("page")
    public ApiResponse<PageResponse<TbUserInfo>> page(@RequestBody @Valid TbUserRequest request) {
        PageResponse<TbUserInfo> response = tbUserInfoService.page(request);
        if (response == null) {
            return returnError(response, "空数据");
        }
        return returnSuccess(response);
    }

}